<template>
    <div class="page-spec-detail" v-if="detail">
        <!-- banner -->
        <VideoBanner v-if="detail"
            :images="[detail.cover]" />
        <!-- 详情 -->
        <div class="info">
            <div class="inner">
                <div class="header">
                    <div class="fl-box">
                        <div class="price-box">
                            <div class="price">¥{{detail.price}}/件</div>
                            <div class="des" v-if="detail.discount !== 100">{{'选择该产品享' + (detail.discount / 10) + '折扣'}}</div>
                        </div>
                        <div class="title normal-text grey-text-color">{{detail.value}}</div>
                    </div>
                    <div class="fr-box">
                        <Stepper :value="detail.number" :val-index="valIndex" @change="onNumberChange" :can-change="canOptionDel" />
                    </div>
                </div>
                <div class="desc">
                    <div class="giant-text">PRODUCT DETAILS</div>
                    <div class="gianttext">产品详情</div>
                </div>
            </div>
        </div>
        <!-- <LineTitle v-if="detail" value="产品详情" :has-pad="true" :container-class="['bg-default']" :line-class="['color-font-4']" /> -->
        <div class="detail-description text-small">
            <RichText :content="description"></RichText>
        </div>
        <!-- <div class="fixed-btn-wrap white-color">
            <div class="price text-normal">
                当前数量：{{detail.number}}
            </div>
            <div class="btn btn-mobile mr-right-20 btn-large btn-white-yellow btn-rect" @click="cancel">取消产品</div>
            <div class="btn btn-mobile btn-large btn-white-red btn-redt" @click="add">添加产品</div>
        </div> -->
    </div>
</template>

<script lang="ts">
import {
    formatRichText,
} from '@/lib/utils';
import VideoBanner from '@/components/common/videoBanner.vue';
import LineTitle from '@/components/common/lineTitle.vue';
import Stepper from '@/features/pkg/stepper.vue';
import store from '@/store';
import { Component, Vue } from 'vue-property-decorator';

@Component({
    name: 'SpecDetail',
    components: {
        VideoBanner,
        LineTitle,
        Stepper,
    },
})
export default class SpecDetail extends Vue {
    public get pkgId() {
        return this.$route.query.pkgId;
    };
    public get specIndex() {
        return this.$route.query.specIndex;
    };
    public get valIndex() {
        return this.$route.query.valIndex;
    };

    public get selectedGoodsSpecMap() {
        return store.state.pkg.selectedGoodsSpecMap;
    }

    public get selectedGoodsSpec() {
        const pkgId = Number(this.pkgId);
        return this.selectedGoodsSpecMap[pkgId];
    }

    public get value() {
        return this.selectedGoodsSpec[Number(this.specIndex)];
    }

    public get detail() {
        return this.selectedGoodsSpec && this.specIndex !== null && this.valIndex !== null ? this.selectedGoodsSpec[Number(this.specIndex)][Number(this.valIndex)] : null;
    }

    public get description() {
        return this.detail && this.detail.description ? formatRichText(this.detail.description) : null;
    }

    public get mustSelectOne() {
        return this.value.some((item: any) => item.is_default);
    }

    public get canOptionDel() {
        return this.value.reduce((prev: any, next: any) => {
            return {
                number: prev.number + next.number,
            };
        }, {
            number: 0,
        }).number > 1 || !this.mustSelectOne;
    }

    public onNumberChange(option: any) {
        const type = option.type;
        const valIndex = option.index;
        const number = option.number;
        store.commit('pkg/setSelectedGoodsSpecItemNumber', {
            id: this.pkgId,
            specIndex: this.specIndex,
            valIndex: valIndex,
            number: type === 'del' ? number - 1 : number + 1,
        });
    }

    public async created() {
        if (!this.detail) {
            wx.navigateBack();
        }
    }
}
</script>

<style lang="less">
@import "../../lib/style/mixins.less";

.page-spec-detail {
    .info {
        padding: 10px 16px 0;
        background-color: @white-color;
        .title {
            padding-bottom: 10px;
        }
        .price-box {
            display: flex;
            // justify-content: space-between;
            align-items: center;
            color: #f4051a;
            .price {
                font-size: 48rpx;
            }
            .des {
                margin-left: 10px;
                font-size: 24rpx;
            }
        }
        .desc {
            padding: 10px 0;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #bbbbbb;
            .fl-box {
                flex: 1;
            }
            .fr-box {
                width: 200rpx;
                display: flex;
                justify-content: flex-end;
            }
        }
    }
    .fixed-btn-wrap {
        padding: 20rpx 40rpx;
        // box-sizing: border-box;
        // position: fixed;
        // left: 0;
        // bottom: 0;
        // width: 100%;
        // padding: 20rpx 24rpx;
        // background: rgba(1, 8, 36, 0.48);
        // display: flex;
        // align-items: stretch;
        // justify-content: flex-end;

        // .price {
        //     display: flex;
        //     flex: 1;
        //     align-items: center;
        //     padding: @button-large-padding-v 14px;
        //     line-height: @button-large-line-height;
        //     box-sizing: border-box;
        //     &.safe {
        //         padding-bottom: @button-large-padding-v + @button-safe-padding-bottom;
        //     }
        // }
        // .btn {
        //     width: 113px;
        //     border-radius: 0;
        //     text-align: center;
        // }
        // .mr-right-20 {
        //     margin-right: 20rpx;
        // }
    }
}

</style>
